package com.example.basecommon.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.WindowManager;

import com.example.basecommon.R;

public class TipsErrorDialog {

    /**
     * @param activity
     * @param title    显示错误类型 例 服务器错误:、网络错误:、
     * @param message
     */
    public static void show(Activity activity, String title, String message) {
        show(activity, title, message, (DialogConfirmCallBack) null);
    }

    public static void show(Activity activity, String title, String message, final DialogConfirmCallBack callBack) {
        try {
            if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.base_common_Dialog_Error_Stype)
                        .setMessage(message)
                        .setTitle(title)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (callBack != null) {
                                    callBack.onConfirm();
                                }
                            }
                        })
                        .setCancelable(false);

                AlertDialog alertDialog = builder.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void show(Activity activity, int resTitleId, int resMessageId) {

        String title = activity.getResources().getString(resTitleId);
        String message = activity.getResources().getString(resMessageId);
        show(activity, title, message);
    }

    public interface OnOkButtonClickListener {
        void onOkButtonClick();
    }

    /**
     * 弹框带监听器
     *
     * @param activity
     * @param title
     * @param message
     * @param listener
     */
    public static void show(Activity activity, String title, String message, final OnOkButtonClickListener listener) {
        if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity.getApplicationContext(), R.style.base_common_Dialog_Error_Stype)
                    .setMessage(message)
                    .setTitle(title)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            listener.onOkButtonClick();
                        }
                    })
                    .setCancelable(false);

            AlertDialog alertDialog = builder.create();
            alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();
        }
    }

    public interface DialogConfirmCallBack {
        void onConfirm();
    }
}
